define(
	[
		'require',
		'echarts/chart/k',
		'apm/ui/business_chart_line'
	],
	function(require){
		var forEach = Ember.EnumerableUtils.forEach;

		var ChartView = require("apm/ui/business_chart_line").extend({
			_translateOption: function(option){
				var indicator = this.get("indicator");

				if(indicator.chart == "k"){
					//k线图
					this._translateXAxis(option);
					this._translateSeries_k(option);
					this._translateOther_k(option);
					this._setFormatter_k(option);
				}else{
					//默认为线图
					this._translateXAxis(option);
					this._translateSeries_line(option);
					this._translateOther_line(option);
					this._setFormatter(option);
				}
			},

			_translateSeries_line: function(option){
				var view = this;
				var data = this.get("data");
				var indicator = this.get("indicator");

				this.set("fmtType", indicator["transform"]);
				if(indicator&&indicator.exts&&indicator.exts.length>0){
					forEach(indicator.exts||[], function(indicator,index){
					var backgroundColor,
						borderColor;
						switch(index){
							case 0:
							  backgroundColor = "rgba(248,206,105,.3)";
							  borderColor = "#f8ce69";
							  break;
							case 1:
							  backgroundColor = "rgba(0,221,255,.36)";
							  borderColor = "#00ddff";
							  break;
							case 2:
							  backgroundColor = "rgba(4,218,148,.3)";
							  borderColor = "#04da94";
							  break;
						}

						var serieConfig = {
							name: indicator.name,
							type:'line',
							
							itemStyle: {normal: {
								borderColor: "#fff",
								borderWidth:1,
								color: borderColor,
								areaStyle: {type: 'default',color:backgroundColor},lineStyle:{width:1,color:"#f8ce69"}}
							},
							symbol: 'circle',
							symbolSize: 3,
							showAllSymbol: true,
							smooth:true,
							data:[]
						};
						option.series.push(serieConfig);

						forEach(data, function(performance){
							serieConfig.data.push(view._translateSeriesDataItem(performance[indicator.code]));
						});
					});
				}else{
					this._translateSeries(option);
				}
			},

			_translateOther_line: function(option){
				var view = this;
				var data = this.get("data");
				var indicator = this.get("indicator");

				this._translateOther(option);

				option.legend.y = 30;
				option.grid.y = 60;
				option.legend.data = [];

				if(indicator&&indicator.exts){
					forEach(indicator.exts||[], function(indicator){
						option.legend.data.push(indicator.name);
					});
				}else{
					option.legend.y = 0;
					option.grid.y = 30;
				}
			},

			_translateSeries_k: function(option){
				var view = this,
					data = this.get("data")||[],
					indicator = this.get("indicator");

				option.series.push({
					name: indicator.name,
					type:'k',
					data:[]
				});

				if(indicator&&indicator.exts&&indicator.exts.length>2){
					var seriesData = option.series[0].data,
						exts = indicator.exts;
					forEach(data, function(pfms, i, arr){
						seriesData.push([
							arr[Math.max(0, i-1)][exts[0].code],
							pfms[exts[0].code],
							pfms[exts[1].code],
							pfms[exts[2].code]
						]);
						if( !Ember.isNone(pfms[exts[0].code]) ){
							view.hasData = true;
						}
					});
				}
			},

			_translateOther_k: function(option){
				this._translateOther(option);
				this.set("fmtType", this.get("indicator")["transform"]);
			},

			_setFormatter_k: function(option){
				var fmtType = this.get("fmtType");
				option.yAxis[0].axisLabel.formatter = APM.util.indicatorFormatter(fmtType);
				option.tooltip.formatter = this._tooltipFormatter_k(fmtType);
			},

			_tooltipFormatter_k: function(type){
				return function(params){
					var param = params[0];
					var formatter = APM.util.indicatorFormatter(type);
					var tooltip = param.name + ' ' + param.seriesName;
					tooltip += '<br/>  上次 : ' + formatter(param.value[0]) + '  最高 : ' + formatter(param.value[3]);
					tooltip += '<br/>  本次 : ' + formatter(param.value[1]) + '  最低 : ' + formatter(param.value[2]);
					return tooltip;
				}
			}
		});

		return ChartView;
	}
);